package cn.zktr.Personnel.model.service.zhouxiong;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.zktr.Personnel.model.dao.zhouxiong.*;
import cn.zktr.Personnel.model.mappers.zhouxiong.*;
import cn.zktr.Personnel.model.pojos.*;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
 
@Service
@Transactional(rollbackOn=Exception.class)
public class JiChuService {
	
	@Autowired
	private CwaShfDAO shfD;			//班次
	@Autowired
	private CwaShfMapper shfM;			//班次
	@Autowired
	private	CwaShfMapper shf;		//班次
	@Autowired
	private CwaTemDAO temD;			//原因
	@Autowired
	private CwaTemMapper tem;		//原因
	@Autowired
	private CwaTypeDAO typeD;		//请假种类
	@Autowired
	private CwaTypeMapper type;		//请假种类
	@Autowired
	private CwaRsnDAO rsnD;			//原因
	@Autowired
	private CwaRsnMapper rsnM;		//原因
	

	@Autowired
	private findService fs;

	//***************************班次******************************
	/**班次
	 * @return
	 */
	public JSONArray selectBanCi() {
		try {
			return shf.seleBanCi();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**根据id 查班次
	 * @param shfid
	 * @return
	 */
	public PerCwaShf findByBanCiId(Integer shfid) {
		try {
			return shfD.findById(shfid).get();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**更新班次
	 * @param shf
	 */
	public String updateBC(Integer shfid,String shfname,String shftime,String shfrem) {
		try {
			if(shfM.seleShfName(shfname).size()>0)
				return "-1";
			PerCwaShf shf = new PerCwaShf();
			if(shfid!=null) {
				shf.setShfid(shfid);
			}
			shf.setShfname(shfname);
			shf.setShftime(shftime);
			shf.setShfrem(shfrem);
			shfD.save(shf);
			return "0";
		} catch (Exception e) {
			return "0";
		}
	}
	
	/**删除班次
	 * @param rsnid
	 */
	public void deleteBC(Integer shfid) {
		try {
			shfD.deleteById(shfid);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	//***************************班组******************************
	/**班组
	 * @return
	 */
	public JSONArray selectBanZu() {
		try {
			return tem.seleBanZu();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**班组
	 * @param temid
	 * @return
	 */
	public PerCwaTem findByBanZuId(Integer temid) {
		try {
			return temD.findById(temid).get();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**更新 班组
	 * @param tem
	 */
	public String updateBZ(Integer temid,Integer deptid,Integer empid,String temname,Integer temrs,String temrem) {
		try {
			if(tem.seleTemName(temname).size()>0)
				return "-1";
			PerCwaTem tem = new PerCwaTem();
			if(temid!=null) {
				tem.setTemid(temid);
			}
			tem.setPerPmsDept(fs.findDeptid(deptid));
			tem.setPerPmsEmp(fs.findEmpid(empid));
			tem.setTemname(temname);
			if(temrs!=null) {
				tem.setTemrs(temrs);
			}else {
				tem.setTemrs(0);
			}
			
			tem.setTemrem(temrem);
			temD.save(tem);
			return "0";
		} catch (Exception e) {
			return "0";
		}
	}
	
	/**删除班组
	 * @param rsnid
	 */
	public void deleteBZ(Integer temid) {
		try {
			temD.deleteById(temid);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	//***************************请假种类******************************
	/**请假种类
	 * @return
	 */
	public JSONArray seleQinJiaZhongLei() {
		try {
			return type.seleQinJiaZhongLei();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**请假种类
	 * @param typeid
	 * @return
	 */
	public PerCwaQType findByQJZongLeiId(Integer typeid) {
		try {
			return typeD.findById(typeid).get();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**更新 请假种类
	 * @param type
	 */
	public void updateZL(PerCwaQType type) {
		try {
			typeD.save(type);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**删除 请假种类
	 * @param rsnid
	 */
	public void deleteZL(Integer typeid) {
		try {
			typeD.deleteById(typeid);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	
	//***********************原因***********************************
	
	/**原因
	 * @return
	 */
	public JSONArray seleYuanYin() {
		try {
			return rsnM.seleYuanYin();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**原因
	 * @param rsnid
	 * @return
	 */
	public PerCwaRsn findByYuanYinId(Integer rsnid) {
		try {
			return rsnD.findById(rsnid).get();
		} catch (Exception e) {
			return null;
		}
	}
	
	/**根据 原因 查原因
	 * @param rsnid
	 * @return
	 */
	public PerCwaRsn findByRsnreason(String RsnName) {
		try {
			return rsnD.findByRsnreason(RsnName);
		} catch (Exception e) {
			return null;
		}
	}

	/**原因 新增修改
	 * @param rsn
	 */
	public void updateYY(Integer rsnid,String rsnreason,String rsnrem) {
		try {
			PerCwaRsn rsn = new PerCwaRsn();
			if(rsnid!=null) {
				rsn.setRsnid(rsnid);
			}
			rsn.setRsnreason(rsnreason);
			rsn.setRsnrem(rsnrem);
			rsnD.save(rsn);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**删除
	 * @param rsnid
	 */
	public void deleteYY(Integer rsnid) {
		try {
			rsnD.deleteById(rsnid);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
